//
//  TripViewController.m
//  XhlTrip
//
//  Created by dllo on 15/8/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "TripViewController.h"
#import "LeaonWorking.h"
#import "TripViewCell.h"
#import "TripModel.h"
#import "MJTableView.h"
#import "TripDetailController.h"
#import "MBProgressHUD.h"


NSInteger j = 2;
@interface TripViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) NSMutableArray *tripArr;
@property (nonatomic, retain) MJTableView *tableview;
@property (nonatomic, retain) NSString *passI;
@property (nonatomic, retain) NSString *numId;
@end

@implementation TripViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tripArr = [NSMutableArray array];
    }
    return self;
}


- (void)dealloc
{
    [_passI release];
    [_numId release];
    [_tableview release];
    [_tripArr release];
    [super dealloc];
}

/* 传过来Id 进行网络请求 */
- (void)passVvalue:(NSString *)pathId
{
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:hud];
    hud.labelText = @"正在加载";
    [hud show:YES];
    self.numId = pathId;
    NSString *path = [NSString stringWithFormat:@"%@%@%@", @"https://chanyouji.com/api/destinations/attractions/", pathId, @".json?page=1" ];
    self.passI = path;

    NSString *nn = [NSString stringWithFormat:@"%@%@", @"trip", pathId];
    [LeaonWorking netWorkingHanderGetGataWithUrl:path pathname:nn resultBlock:^(id result) {
      
        [self.tripArr addObjectsFromArray:result];
        [self.tableview reloadData];
        [hud removeFromSuperview];
        [hud release];
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
}

- (void)refreshingHeader:(id)tableView {
    NSLog(@"下拉刷新");

    NSString *nn = [NSString stringWithFormat:@"%@%@", @"trip", self.passI];
    [LeaonWorking netWorkingHanderGetGataWithUrl:self.passI pathname:nn resultBlock:^(id result) {
        [self.tripArr removeAllObjects];
        [self.tripArr addObjectsFromArray:result];
        
        /* tableview 重新加载数据 */
        [_tableview reloadData];
    }];
    
}

- (void)refreshingFooter:(id)tableView {
    NSLog(@"上拉加载");
    
    NSString *page = [NSString stringWithFormat:@"%ld", (long)j];
    NSString *str = [NSString stringWithFormat:@"%@%@", @"https://chanyouji.com/api/destinations/attractions/55.json?page=", page];
    NSString *ss = [NSString stringWithFormat:@"%@%@", @"trip", page];
    [LeaonWorking netWorkingHanderGetGataWithUrl:str pathname:ss resultBlock:^(id result) {
        [self.tripArr addObjectsFromArray:result];
        
        /* tableview 重新加载数据 */
        [_tableview reloadData];

    }];
    
    j = j + 1;
    
}

- (void)createTableView
{
    self.tableview = [[MJTableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain delegate:self];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    [self.view addSubview:self.tableview];
    _tableview.rowHeight = 110 * Height;
    
    [_tableview release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tripArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TripViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"trip"];
    if (cell == nil) {
        cell = [[[TripViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"trip"] autorelease];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    TripModel *mm = [[[TripModel alloc] init] autorelease];

    [mm setValuesForKeysWithDictionary:self.tripArr[indexPath.row]];
     cell.tripModel = mm;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TripDetailController *tripDetail = [[TripDetailController alloc] init];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"旅行地";
    self.navigationItem.backBarButtonItem = backItem;
    [backItem release];
    [tripDetail passNetId:[self.tripArr[indexPath.row] objectForKey:@"id"] nsstring:[self.tripArr[indexPath.row] objectForKey:@"description"]];
    
    [self.navigationController pushViewController:tripDetail animated:YES];
    [tripDetail release];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
